/*jshint esversion: 9 */
/**
 * 元素事件小工具
 */
export function scrollDirect({target,fn,upFn,downFn,leftFn,rightFn}={}){  //判断滚动条滚动方向
    let newY = 0;
    let oldY = 0;
    let newX = 0;
    let oldX = 0;
    target.addEventListener("scroll",function(){
        newY = target==window?this.scrollY:this.scrollTop;
        newX = target==window?this.scrollX:this.scrollLeft;
        if(fn){
            fn({newX,newY,oldX,oldY});
        }
        if(newY>oldY){
            if(downFn){
                downFn({newX,newY,oldX,oldY});
            }
        }else{
            if(upFn){
                upFn({newX,newY,oldX,oldY});
            }
        }
        if(newX>oldX){
            if(rightFn){
                rightFn({newX,newY,oldX,oldY});
            }
        }else{
            if(leftFn){
                leftFn({newX,newY,oldX,oldY});
            }
        }
        oldY = newY;
        oldX = newX;
    });
}
/**
 * 元素触摸事件，包括鼠标拖动，以及整个滑动到结束时的滑动速率
 * 只限鼠标左键拖动
 */
export function touchEvent({
        target,touchFn,
        hasSpeedX=false,hasSpeedY=false,
        speedFn,
    }={}){
    let mouseDown = false;  //鼠标是否按下
    let oldTouchEvent;  //上一次滑动事件
    let oldSpeedTime = 0;  //上一次速率时间
    let oldSpeedEvent;  //上一次速率事件
    let speedSpaceTimer;  //记录更新事件的任务
    let moveLength = 0;  //滑动的长度
    const onMove = (e)=>{  //滑动事件，包括鼠标拖动
        moveLength++;
        let oldX = oldTouchEvent.clientX;
        let oldY = oldTouchEvent.clientY;
        let newX = e.clientX;
        let newY = e.clientY;
        if(touchFn){
            touchFn({oldX,oldY,newX,newY});
        }
        oldTouchEvent = e;
    };
    const updateSpeedData = (e)=>{  //间隔时间更新速率事件以及时间
        if(!hasSpeedX && !hasSpeedY) return;
        let newTime = new Date().getTime();
        speedSpaceTimer = setTimeout(()=>{
            oldSpeedTime = newTime;
            oldSpeedEvent = e;
        },30);
    };
    const computedSpeed = (e)=>{  //计算滑动速率
        clearTimeout(speedSpaceTimer);
        if(!speedFn) return;
        let speedX = 0;  //X速率
        let speedY = 0;
        let newTime = 0;
        let oldX = oldSpeedEvent.clientX;
        let oldY = oldSpeedEvent.clientY;
        let newX = e.clientX;
        let newY = e.clientY;
        if(hasSpeedX || hasSpeedY){
            newTime = new Date().getTime();
        }
        let time_ = newTime - oldSpeedTime;
        if(hasSpeedX && time_ !== 0){
            speedX = Math.abs(newX - oldX)/time_;
        }
        if(hasSpeedY && time_ !== 0){
            speedY = Math.abs(newY - oldY)/time_;
        }
        speedFn({speedX,speedY});
        oldSpeedTime = newTime;
        oldSpeedEvent = e;
    };
    //移动端滑动事件
    target.addEventListener('touchmove',e=>{
        onMove(e.touches[0]);
        updateSpeedData(e.touches[0]);
    });
    target.addEventListener('touchstart',e=>{
        oldTouchEvent = e.touches[0];
        oldSpeedEvent = e.touches[0];
        if(hasSpeedX || hasSpeedY){
            oldSpeedTime = new Date().getTime();
        }
    });
    target.addEventListener('touchend',e=>{
        computedSpeed(e.touches[0]);
        setTimeout(()=>{
            moveLength = 0;
        },0);
    });
    target.style.userSelect = 'none';  //阻止内容复制
    //pc端事件
    target.addEventListener('mousemove',e=>{
        if(!mouseDown) return;
        e.preventDefault();  //阻止内容复制
        onMove(e);
        updateSpeedData(e);
    });
    target.addEventListener('mouseleave',e=>{
        if(!mouseDown) return;
        mouseDown = false;
        computedSpeed(e);
        setTimeout(()=>{
            moveLength = 0;
        },0);
    });
    target.addEventListener('mousedown',e=>{
        if(e.button !== 0) return;  //必须是鼠标左键
        oldTouchEvent = e;
        oldSpeedEvent = e;
        if(hasSpeedX || hasSpeedY){
            oldSpeedTime = new Date().getTime();
        }
        mouseDown = true;
    });
    target.addEventListener('mouseup',e=>{
        if(e.button !== 0) return;  //必须是鼠标左键
        mouseDown = false;
        computedSpeed(e);
        setTimeout(()=>{
            moveLength = 0;
        },0);
    });
    target.addEventListener('click',(e)=>{  //滑动时禁止点击事件(该事件在mouseup后)
        if(e.button !== 0) return;  //必须是鼠标左键
        if(moveLength>3){
            e.preventDefault();
            e.stopPropagation();
        }
    },true);
}